package org.jeecg.modules.project.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.project.entity.BimProjectBuild;
import org.jeecg.modules.project.entity.BimProjectFloor;
import org.jeecg.modules.project.entity.BuildResultType;
import org.jeecg.modules.project.model.result.BimProjectBuildInfo;
import org.jeecg.modules.project.model.result.BimProjectFloorResult;
import org.jeecg.modules.project.service.IBimProjectBuildService;
import org.jeecg.modules.project.service.IBimProjectFloorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: bim_project_build
 * @Author: jeecg-boot
 * @Date: 2023-04-13
 * @Version: V1.0
 */
@Api(tags = "楼栋管理")
@RestController
@RequestMapping("/bim/build")
@Slf4j
public class BimProjectBuildController extends JeecgController<BimProjectBuild, IBimProjectBuildService> {

    @Autowired
    private IBimProjectBuildService bimProjectBuildService;

    @Autowired
    private IBimProjectFloorService bimProjectFloorService;

    /**
     * 分页列表查询
     *
     * @param bimProjectBuild
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "楼栋分页列表查询")
    @ApiOperation(value = "楼栋分页列表查询", notes = "楼栋分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BimProjectBuild bimProjectBuild,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BimProjectBuild> queryWrapper = QueryGenerator.initQueryWrapper(bimProjectBuild, req.getParameterMap());
        Page<BimProjectBuild> page = new Page<>(pageNo, pageSize);
        IPage<BimProjectBuild> pageList = bimProjectBuildService.page(page, queryWrapper);
        Page<BimProjectBuildInfo> pageResList = new Page<>(pageNo, pageSize);
        if (CollUtil.isNotEmpty(pageList.getRecords())) {
            List<BimProjectBuildInfo> buildInfoList = new ArrayList<>();
            pageList.getRecords().stream().forEach(item -> {
                BimProjectBuildInfo buildInfo = new BimProjectBuildInfo();
                BeanUtils.copyProperties(item, buildInfo);
                BimProjectFloor floor = new BimProjectFloor();
                floor.setBuildId(item.getBuildId());
                buildInfo.setFloors(this.convert(bimProjectFloorService.queryFloor(floor, BuildResultType.COMMON)));
                buildInfoList.add(buildInfo);
            });
            pageResList.setTotal(page.getTotal());
            pageResList.setPages(page.getPages());
            pageResList.setRecords(buildInfoList);
        }
        return Result.OK(pageResList);
    }

    private List<BimProjectFloorResult> convert(List<BimProjectFloor> queryFloors) {
        List<BimProjectFloorResult> floorResults = new ArrayList<>();
        if (CollUtil.isNotEmpty(queryFloors)) {
            queryFloors.stream().forEach(item -> {
                BimProjectFloorResult floorResult = new BimProjectFloorResult();
                floorResult.setBuildId(item.getBuildId());
                floorResult.setFloorId(item.getFloorId());
                floorResult.setFloorNo(item.getFloorNo());
                if (StringUtils.isNotBlank(item.getMaps())) {
                    floorResult.setMaps(JSONUtil.parseObj(item.getMaps()));
                }
                floorResults.add(floorResult);
            });
        }
        return floorResults;
    }

    /**
     * 添加
     *
     * @param bimBuildList
     * @return
     */
    @AutoLog(value = "楼栋添加")
    @ApiOperation(value = "楼栋添加", notes = "楼栋添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Valid List<BimProjectBuild> bimBuildList) {
        List<BimProjectBuild> resBimBuildList = bimProjectBuildService.add(bimBuildList);
        return Result.OK(resBimBuildList);
    }

    /**
     * 编辑
     *
     * @param bimProjectBuild
     * @return
     */
    @AutoLog(value = "楼栋编辑")
    @ApiOperation(value = "楼栋编辑", notes = "楼栋编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BimProjectBuild bimProjectBuild) {
        bimProjectBuildService.edit(bimProjectBuild);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "楼栋通过id删除")
    @ApiOperation(value = "楼栋通过id删除", notes = "楼栋通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        bimProjectBuildService.delete(id);
        return Result.OK("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "楼栋通过id查询")
    @ApiOperation(value = "楼栋通过id查询", notes = "楼栋通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BimProjectBuild bimProjectBuild = bimProjectBuildService.getById(id);
        if (bimProjectBuild == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(bimProjectBuild);
    }

}
